export default {
    namespaced:true,
    state:{
        unreadCount:0,//未读消息数
        customer: {
            count:0,
            content:"暂无新消息",
            lastMessageTime:'',
        }
    },
    mutations: {
        add(state,payload){
            state.unreadCount=payload
        },
        //websocke接收到一条自增
        add_customer_count(state){
            state.customer.count+=1;
        },
        //飞手上线更新客服消息条数
        update_customer_count(state,payload){
            state.customer.count=payload.count
            state.customer.content=payload.content
        },
        update_customer(state,payload){
            state.customer.content=payload.content
            state.customer.lastMessageTime=payload.lastMessageTime
        },
        update_tabs(state){
            // if(state.unreadCount>0){
            //     uni.setTabBarBadge({
            //         index: 1,
            //         text: state.unreadCount > 99 ? '...' : String(state.unreadCount)
            //     });
            // }else{
            //     uni.removeTabBarBadge({
            //         index: 1
            //     });
            // }
        },
        clear_customer_count(state){
            state.customer.count=0;
        }
    },
    actions: {
        addUnreadCountAction(context,payload){
            context.commit('add',payload)
        },
        updateMessage({ commit, state }) {
            uni.$uv.http.get('/socketMsg/countUnreadMessages', { params: { theme: uni.getStorageSync('login-type') } }).then(res => {
                commit("add",res.data.customerMessage + res.data.messageCenter)
                let content="暂无新消息";
                if(res.data.customerMessage>0)
                    content="您有一条新信息"
                let load={
                    count:res.data.customerMessage,
                    content
                }
                commit("update_customer_count",load)
                commit("update_tabs")
            }).catch(error => {
                console.error(error);
            });
        }
    },
    getters: {}
}
